<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;width:100%">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hADD">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all=""
        >
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hADD(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="del(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
    >
      <dept-dialog v-if="showDialog" :id="curId" :is-edit="isEdit" :origin-list="originList" @success="hsuccess" @hclose="hclose" />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartmentList, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import DeptDialog from './deptDialog.vue'

export default {
  components: {
    DeptDialog

  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      // 点击编辑列表是否处于编辑状态
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    // 查询企业的部门列表,更新列表
    async  loadDepartmentList() {
      try {
        const res = await getDepartmentList()
        // console.log(res)
        // 把第一个样板删除
        res.data.depts.shift(0, 1)
        // 渲染列表。但是需要是树形结构的。
        // 重新封装一个组件创建树形结构方法，在这里调用
        // 在父组件封装好要用的数据，在子组件里调用做表单校验
        this.originList = res.data.depts.map(({ id, code, name, pid }) => ({ id, code, name, pid }))
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log(e)
      }
    },
    // 点击添加子部门，跳出弹框
    // 点击添加子部门，传参，获取id的值，传给子组件
    hADD(id) {
      this.showDialog = true
      // 需要传到子组件的id
      this.curId = id
      this.isEdit = false
    },
    // 子组件触发父组件事件
    // 添加完成后，关闭弹框，获取信息
    hsuccess() {
      this.showDialog = false
      this.loadDepartmentList()
      this.isEdit = false
    },
    // 点击取消，关闭弹框
    hclose() {
      this.showDialog = false
      this.loadDepartmentList()
      this.isEdit = false
    },
    // 点击编辑子部门，跳出弹框
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 点击删除
    del(id) {
      this.$confirm('确定删除吗？嘤嘤嘤', '提示', { type: 'warning' })
        .then(async() => {
          try {
            const res = await delDepartment(id)
            this.$message.success(res.message)
            this.loadDepartmentList()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    }
  }
}
</script>
